package fi.piet.ec2.department;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import fi.piet.ec2.HomePage;
import fi.piet.ec2.service.DepartmentService;

/**
 * DepartmentPage
 */
public class DepartmentPage extends WebPage {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private DepartmentService departmentService;

	/**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
	public DepartmentPage(final PageParameters parameters) {
		List<Department> departmentList = new ArrayList<Department>(
				departmentService.getAllDepartments());
		Department department = new Department();
		add(new DepartmentForm("addDepartmentForm", department));

		add(new ListView("departmentList", departmentList) {

			private static final long serialVersionUID = 6900644892131565325L;

			@SuppressWarnings("serial")
			public void populateItem(final ListItem item) {
				final Department depa = (Department) item.getModelObject();
				item.add(new Label("name", depa.getName()));
				Link edit = new Link("edit") {
					@Override
					public void onClick() {
						EditDepartment edep = new EditDepartment(depa);
						setResponsePage(edep);
					}
				};
				item.add(edit);
			}
		});

		add(new PageLink("homePageLink", HomePage.class));
	}
}
